package com.wteam.dragon.system.aop.log.controller.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.system.aop.log.controller.pojo.bean.Log;
import com.wteam.dragon.system.aop.log.controller.pojo.dto.LogErrorDTO;
import com.wteam.dragon.system.aop.log.controller.service.LogQueryService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import com.wteam.dragon.system.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 陈晓辉
 * @date 2020-03-12
 */
@RestController
@RequestMapping("log")
@Api(tags = "系统：后台日志管理")
@RequiredArgsConstructor
public class LogController {
    private final LogQueryService logQueryService;

    @com.wteam.dragon.system.aop.log.log.Log("分页获得日志列表")
    @GetMapping("pageLog")
    @ApiOperation("分页获得日志列表")
    @PreAuthorize("@el.check('pageLog')")
    public ResultMessage pageLog(Page<Log> page){
        Log log = new Log();
        log.setLogType("INFO");
        return logQueryService.pageLog(page, log);
    }

    @ApiOperation("获得当前用户的日志")
    @com.wteam.dragon.system.aop.log.log.Log("获得当前用户的日志")
    @GetMapping(value = "pageUserLog")
    @PreAuthorize("@el.check('pageUserLog')")
    public ResultMessage pageUserLog(Page<Log> page){
        Log log = new Log();
        log.setLogType("INFO");
        log.setUsername(UserUtil.getCurrentUsername());
        return logQueryService.pageUserLog(page, log);
    }

    @ApiOperation("获得报错日志")
    @com.wteam.dragon.system.aop.log.log.Log("获得报错日志")
    @GetMapping(value = "pageErrorLog")
    @PreAuthorize("@el.check('pageErrorLog')")
    public ResultMessage pageErrorLog(PageVo<LogErrorDTO> pageVo){
        Page<LogErrorDTO> page = BeanUtil.fromPage(pageVo);
        Log log = new Log();
        log.setLogType("ERROR");
        return logQueryService.pageErrorLog(page, log);
    }

    @ApiOperation("通过id获得报错信息的详情")
    @com.wteam.dragon.system.aop.log.log.Log("通过id获得报错信息的详情")
    @GetMapping(value = "getErrorDetail")
    @PreAuthorize("@el.check('getErrorDetail')")
    public ResultMessage getErrorDetail(Long id){
        return logQueryService.getErrorDetail(id);
    }
}
